package com.tz.crm.service.impl;

import com.tz.crm.dao.ContractDao;
import com.tz.crm.dao.PropertyValueDao;
import com.tz.crm.dao.UserDao;
import com.tz.crm.entity.Contract;
import com.tz.crm.entity.PropertyValue;
import com.tz.crm.service.PropertyValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * PropertyValueServiceImpl 实现类
 * 合同的查找
 */
@Service
@Transactional
public class PropertyValueServiceImpl extends BaseServiceImpl<PropertyValue, String> implements PropertyValueService {

    @Autowired
    private PropertyValueDao propertyValueDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private ContractDao contractDao;

    /**
     * 有参构造
     *
     * @param propertyValueDao 注入的组件
     */
    @Autowired
    public PropertyValueServiceImpl(PropertyValueDao propertyValueDao) {
        this.propertyValueDao = propertyValueDao;
        super.setBaseDao(propertyValueDao);
    }

    @Override
    public Contract savePropertyValueList(List<PropertyValue> propertyValues, Contract contract) {
        contract.setSignUser(userDao.get("realName", contract.getSignUser().getRealName()));
        contractDao.save(contract);
        if (propertyValues != null) {
            int i = 1;
            for (PropertyValue pv : propertyValues) {
                pv.setContract(contract);
                pv.setSeq(i);
                propertyValueDao.save(pv);
                i++;
            }
        }
        return contract;
    }

    @Override
    public List<PropertyValue> getByContractId(String id) {
        return propertyValueDao.getByContractId(id);
    }

}
